package com.bailian.app.controller.Apis;

import com.bailian.common.annotation.Anonymous;
import com.bailian.common.core.controller.BaseController;
import com.bailian.common.core.domain.AjaxResult;
import com.bailian.system.domain.BlBanner;
import com.bailian.system.domain.BlPark;
import com.bailian.system.domain.BlWarehouse;
import com.bailian.system.domain.BlNews;
import com.bailian.system.domain.vo.BlParkVo;
import com.bailian.system.service.IBlBannerService;
import com.bailian.system.service.IBlParkService;
import com.bailian.system.service.IBlWarehouseService;
import com.bailian.system.service.IBlNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手机端通用控制器
 * 
 * @author bailian
 */
@Api(tags = "手机端通用功能接口")
@RestController
@RequestMapping("/app/api/common")
public class AppCommonController extends BaseController {

    @Autowired
    private IBlBannerService bannerService;
    
    @Autowired
    private IBlParkService parkService;
    
    @Autowired
    private IBlWarehouseService warehouseService;
    
    @Autowired
    private IBlNewsService newsService;

    /**
     * 获取Banner列表
     */
    @Anonymous
    @ApiOperation("获取Banner列表")
    @GetMapping("/banner/list")
    public AjaxResult getBannerList(
            @ApiParam("页面类型") @RequestParam(defaultValue = "home") String bannerPage) {
        try {
            BlBanner banner = new BlBanner();
            banner.setBannerPage(bannerPage);
            banner.setStatus(1L); // 只获取启用的Banner
            
            List<BlBanner> list = bannerService.selectBlBannerList(banner);
            return success(list);
        } catch (Exception e) {
            return error("获取Banner列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 搜索接口
     */
    @Anonymous
    @ApiOperation("全局搜索")
    @GetMapping("/search")
    public AjaxResult search(
            @ApiParam("搜索关键词") @RequestParam String keyword,
            @ApiParam("搜索类型") @RequestParam(defaultValue = "all") String type) {
        try {
            Map<String, Object> result = new HashMap<>();
            
            if ("all".equals(type) || "park".equals(type)) {
                // 搜索园区
                BlPark parkQuery = new BlPark();
                parkQuery.setParkName(keyword);
                List<BlParkVo> parks = parkService.selectList(parkQuery);
                result.put("parks", parks);
            }
            
            if ("all".equals(type) || "warehouse".equals(type)) {
                // 搜索仓库
                BlWarehouse warehouseQuery = new BlWarehouse();
                warehouseQuery.setWarehouseName(keyword);
                List<BlWarehouse> warehouses = warehouseService.selectBlWarehouseList(warehouseQuery);
                result.put("warehouses", warehouses);
            }
            
            if ("all".equals(type) || "news".equals(type)) {
                // 搜索资讯
                BlNews newsQuery = new BlNews();
                newsQuery.setTitle(keyword);
                List<BlNews> newsList = newsService.selectBlNewsList(newsQuery);
                result.put("news", newsList);
            }
            
            return success(result);
        } catch (Exception e) {
            return error("搜索失败：" + e.getMessage());
        }
    }
} 